import { patch } from './vnode/patch'
import watcher from './observer/watcher'
// 页面挂载
export function mountComponent(vm, el) {
    callHook(vm, 'beforeMount')

    let updataComponent = () => {
        // 虚拟DOM转DOM
        vm._updata(vm._render())
    }

    // 处理数据更新-双向数据绑定判定
    new watcher(vm, updataComponent, () => {
        callHook(vm, 'updated')
    }, true)

    callHook(vm, 'mounted')
}

// 生命周期
export function lifecycleMixin(Vue) {
    // 增加生命周期
    Vue.prototype._updata = function (vnode) {
        let vm = this
        let preVnode = vm._vnode
        if (!preVnode) {
            vm.$el = patch(vm.$el, vnode)
        }else{
            patch(preVnode, vnode)
        }
        vm._vnode = vnode
    }
}

// 执行生命周期钩子
export function callHook(vm, hook) {
    const handlers = vm.$options[hook]
    if (handlers) {
        for (let i = 0; i < handlers.length; i++) {
            handlers[i].call(this)
        }
    }
}